#include <stdio.h>
#include <string.h>
#include <malloc.h>

void senrev(char *);
void reverstr(char *);
void reverstr1(char *);

int main(int argc,char **argv)
{
	char *input , *out;

	input = "This is the first time to learn English";
	printf("%ld:%s\n",strlen(input),input);

	out = malloc(strlen(input) + 1);
	if(out == NULL) 
		printf("malloc error\n");

	memcpy(out, input, strlen(input) + 1);
	senrev(out);
	printf("%ld:%s\n",strlen(out),out);
	if(out) free(out);

	return 0;
}


void senrev(char * str)
{
	char *sep = " ";	
	char *token, *tmp;

	tmp = str;
	token = strtok(strdup(str),sep);
	while(token != NULL)
	{
		reverstr1(token);
		strncpy(tmp, token, strlen(token));
		tmp += strlen(token);
		token = strtok(NULL,sep);
		*tmp++ = token ? *sep : '\0';
	}
}


void reverstr(char * str)
{
	char *ts, *te, tmp;
	int i;

	i = strlen(str);
	if(i <= 0) return ;

	ts = str;
	te = str + i - 1;

	while(ts < te)
	{
		tmp = *ts;
		*ts = *te;
		*te = tmp;
		ts++;
		te--;
	}
}

void reverstr1(char * str)
{
	char *ts, *te;

	ts = str;
	te = str + strlen(str) - 1;

	while(ts < te)
	{
		*ts = *ts ^ *te;
		*te = *ts ^ *te;
		*ts = *ts ^ *te;
		ts++;
		te--;
	}
}
